package com.markedquilt.homepage.service.impl;

import com.markedquilt.homepage.CourseInfo;
import com.markedquilt.homepage.CourseInfosRequest;
import com.markedquilt.homepage.UserInfo;
import com.markedquilt.homepage.client.CourseClient;
import com.markedquilt.homepage.dao.HomePageUserCourseDao;
import com.markedquilt.homepage.dao.HomePageUserDao;
import com.markedquilt.homepage.entity.HomePageUser;
import com.markedquilt.homepage.entity.HomePageUserCourse;
import com.markedquilt.homepage.service.IUserService;
import com.markedquilt.homepage.vo.CreateUserRequest;
import com.markedquilt.homepage.vo.UserCourseInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ZhangKe
 * @Description
 * @date 2020/12/27 19:34.
 */
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private HomePageUserDao homePageUserDao;

    @Autowired
    private HomePageUserCourseDao homePageUserCourseDao;

    @Autowired
    private CourseClient courseClient;

    @Override
    public UserInfo createUser(CreateUserRequest request) {
        if (!request.validate()){
            return UserInfo.invalid();
        }
        HomePageUser user = homePageUserDao.findByUserName(request.getUserName());
        if (null!= user){
            //已存在的用户
            return UserInfo.invalid();
        }
        HomePageUser newUser = homePageUserDao.save(new HomePageUser(
                request.getUserName(), request.getEmail()
        ));

        return new UserInfo(newUser.getId(), newUser.getUserName(), newUser.getEmail());
    }

    @Override
    public UserInfo getUserInfo(Long id) {
        Optional<HomePageUser> isExist = homePageUserDao.findById(id);
        if (!isExist.isPresent()){
            return UserInfo.invalid();
        }
        HomePageUser user = isExist.get();

        return new UserInfo(user.getId(), user.getUserName(), user.getEmail());
    }

    @Override
    public UserCourseInfo getUserCourseInfo(Long id) {
        Optional<HomePageUser> isExist = homePageUserDao.findById(id);
        if (!isExist.isPresent()){
            return UserCourseInfo.valid();
        }
        HomePageUser user = isExist.get();
        UserInfo userInfo = new UserInfo(user.getId(), user.getUserName(), user.getEmail());

        List<HomePageUserCourse> userCourses = homePageUserCourseDao.findAllByUserId(id);
        if (CollectionUtils.isEmpty(userCourses)){
            return new UserCourseInfo(userInfo, Collections.emptyList());
        }
        List<CourseInfo> courseInfos = courseClient.getCourseInfos(
                new CourseInfosRequest(userCourses.stream()
                                .map(HomePageUserCourse::getCourseId)
                                .collect(Collectors.toList()))
        );
        return new UserCourseInfo(userInfo,courseInfos);
    }
}
